//
//  HomeViewController.swift
//  CBLearnDY
//
//  Created by ncimall on 2017/11/16.
//  Copyright © 2017年 ncimall. All rights reserved.
//

import UIKit

private let kTitleViewH : CGFloat = 40

class HomeViewController: UIViewController {
    
    // MARK:- 懒加载
    private lazy var pageTitleView : PageTitleView = { [weak self] in
        let rect = CGRect.init(x: 0, y: kxStatusBarH + kxNavigationBarH, width: kxScreenW, height: kTitleViewH)
        let pageTitles = ["推荐", "游戏", "娱乐", "趣玩"]
        let pageTitleView = PageTitleView.init(frame: rect, pageTitles: pageTitles)
        pageTitleView.delegate = self
        
        return pageTitleView
    }()
    
    private lazy var pageContentView : PageContentView = { [weak self] in
        let contentH = kxScreenH - kxStatusBarH - kxNavigationBarH - kTitleViewH
        let contentFrame = CGRect.init(x: 0, y: kxStatusBarH + kxNavigationBarH + kTitleViewH, width: kxScreenW, height: contentH)
        
        var childVCs = [UIViewController]()
        for _ in 0..<4 {
            let vc = UIViewController.init()
            vc.view.backgroundColor = UIColor.init(r: CGFloat(arc4random_uniform(255)), g: CGFloat(arc4random_uniform(255)), b: CGFloat(arc4random_uniform(255)))
            childVCs.append(vc)
        }
        
        let pageContentView = PageContentView.init(frame: contentFrame, childVCs: childVCs, parentVC: self)
        pageContentView.delegate = self
        
        return pageContentView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        //  设置UI
        setupUI()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension HomeViewController {
    //  设置UI
    func setupUI() {
        //  设置导航栏
        setupNavigationBar()
        
        //  添加titleView
        view.addSubview(pageTitleView)
        
        //  添加contentView
        view.addSubview(pageContentView)
    }
    
    //  设置导航栏
    func setupNavigationBar() {
        //  设置左侧
        navigationItem.leftBarButtonItem = UIBarButtonItem.init(imageName: "logo", rect: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        
        //  设置右侧
        let rect = CGRect.init(x: 0, y: 0, width: 30, height: 30)
        
        let scanItem = UIBarButtonItem.init(imageName: "Image_scan", highImageName: "Image_scan_click", rect: rect)
        let searchItem = UIBarButtonItem.init(imageName: "btn_search", highImageName: "btn_search_clicked", rect: rect)
        let historyItem = UIBarButtonItem.init(imageName: "image_my_history", highImageName: "Image_my_history_click", rect: rect)
        
        navigationItem.rightBarButtonItems = [historyItem, searchItem, scanItem]
    }
}

// MARK:- 遵守PageTitleViewDelegate
extension HomeViewController: PageTitleViewDelegate {
    func pageTitleViewClicked(titleView: PageTitleView, selectIndex index: Int) {
        pageContentView.setCurrentIndex(currentIndex: index)
    }
}

//  MARK:- PageContentViewDelegate
extension HomeViewController: PageContentViewDelegate {
    func pageContentViewResponse(pageContentView: PageContentView, currentIndex: Int) {
        pageTitleView.changeCurrentSelectState(selectIndex: currentIndex)
    }
}
